//
//  VisitorLoginView.swift
//  weibo-anpeng
//
//  Created by 安鹏 on 16/1/11.
//  Copyright © 2016年 安鹏. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    optional func userWillRegister()
    func userWillLogin()
}

class VisitorLoginView: UIView {

    weak var visitorViewDelegate: VisitorLoginViewDelegate?
    
    @objc private func loginBtnDidClick() {
        visitorViewDelegate?.userWillLogin()
    }
    
    @objc private func registerBtnDidClick() {
        visitorViewDelegate?.userWillRegister?()
    }
    
    func setupUIWithInfo(imageName: String?, tipText: String){
        if let img = imageName {
            circleIcon.image = UIImage(named: img)
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        } else{
            startAnimation()
        }
        
        tipLabel.text = tipText
    }
    
    func startAnimation() {
        let base = CABasicAnimation(keyPath: "transform.rotation")
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        base.duration = 1
        base.removedOnCompletion = false
        circleIcon.layer.addAnimation(base, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(circleIcon)
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //添加约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(225)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right .equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        backView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn)
        }
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }

    // 懒加载设置子视图
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(14)
        l.text = "登陆后!别人评论的你的微博微博微博微博aaaaaaaaaaaaa"
        l.numberOfLines = 0
        l.textColor = UIColor.darkGrayColor()
        l.textAlignment = .Center
        return l
    }()
    
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn
    }()
    
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        let image = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        return btn
    }()
}
